package SwingView.Assignment;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import SwingController.CmsPresenter;
import SwingModel.CmsAssignment;
import SwingModel.CmsQuiz;
import SwingView.CmsContentPanelModel;
import SwingView.Quiz.ALQuizDelete;
import SwingView.Quiz.ALQuizEdit;
import SwingView.Quiz.ALQuizView;
import SwingView.Quiz.CmsCPQuizMainInstructor.QuizEntryRow;
import SwingView.Quiz.CmsCPQuizMainInstructor.QuizEntryRow.ShowDetailActionListener;

public class CmsCPAsgmMainInstructor extends CmsContentPanelModel{

	private JLabel jlTitle;
	private JButton jbtNewAssignment;
	private JPanel jpContent;

	public CmsCPAsgmMainInstructor(){
		//helper panel to center the title panel
		JPanel jp = new JPanel();
		jp.setPreferredSize(new Dimension(600,65));
		jp.setOpaque(false);
		jp.setLayout( new GridBagLayout());
		jp.setBorder(BorderFactory.createEmptyBorder());
		
		//instantiate People title
		jlTitle = new JLabel("Assignment");
		jlTitle.setFont(jlTitle.getFont().deriveFont(20.0f).deriveFont(Font.BOLD));
		jlTitle.setAlignmentY(CENTER_ALIGNMENT);
		jlTitle.setAlignmentY(CENTER_ALIGNMENT);
		jlTitle.setBorder(BorderFactory.createEmptyBorder(0, 8, 10, 8));
		GridBagConstraints c = new GridBagConstraints();
		c.gridy = 0;
		c.anchor = GridBagConstraints.CENTER;
		jp.add(jlTitle,c);
		
		//new quiz button
		jbtNewAssignment = new JButton ("+ Post New Assignment");
		jbtNewAssignment.setFont(jbtNewAssignment.getFont().deriveFont(13.0f).deriveFont(Font.BOLD));
		jbtNewAssignment.setForeground(Color.BLUE);
		jbtNewAssignment.setOpaque(false);
		jbtNewAssignment.setCursor(new Cursor(Cursor.HAND_CURSOR));
		jbtNewAssignment.setBorder(BorderFactory.createEmptyBorder(0,8,0,8));
		c = new GridBagConstraints();
		c.gridy = 1;
		c.insets = new Insets(0, 0, 10, 0);
		c.anchor = GridBagConstraints.LINE_START;
		c.weightx = 1.0;
		c.weighty = 1.0;
		jp.add(jbtNewAssignment,c);
		jbtNewAssignment.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				CmsPresenter.mainView.displayAssignmentComposerPage();
			}
			
		});
		
		//content panel
		jpContent = new JPanel();
		jpContent.setLayout(new GridBagLayout());
		jpContent.setOpaque(false);
		
		//Set up the scroll bar
		JScrollPane scroll = new JScrollPane(jpContent);
		scroll.setBorder(BorderFactory.createEmptyBorder());
		scroll.setOpaque(true);
		scroll.setPreferredSize(new Dimension(650,450));
		scroll.setBackground(Color.WHITE);
		
		//set up student lecture panel and add components
		setLayout(new BorderLayout());
		add(jp,BorderLayout.NORTH);//Add title People
		add(scroll,BorderLayout.WEST);//Add People List
	}
	
	public void displayListOfAssignment(List<CmsAssignment> listOfAsgm){
		jpContent.removeAll();
		jpContent.revalidate();
		jpContent.repaint();
		int i = 0;
		for(CmsAssignment asgm : listOfAsgm){
			GridBagConstraints c = new GridBagConstraints();
			c.gridy = i;
			c.insets = new Insets(0, 0, 8, 0);
			jpContent.add(new AsgmEntryRow(asgm),c);
			i++;
		}
		
		//empty space at bottom - used to fill the bottom of the page
		//when the list is too short
		JPanel p = new JPanel();
		p.setOpaque(false);
		p.setBackground(Color.WHITE);
		GridBagConstraints c_p = new GridBagConstraints();
		c_p.gridy = i;
		c_p.fill = GridBagConstraints.VERTICAL;
		c_p.weightx=1.0;
		c_p.weighty=1.0;
		jpContent.add(p,c_p);
		
		jpContent.repaint();
		jpContent.revalidate();
	}
	
	public class AsgmEntryRow extends JPanel{
		
		
		final static private int ROW_WIDTH = 580;
		final static private int ROW_HEIGHT = 80;
		
		final static private int DATE_NAME_COLUMN_SIZE = 100;
		private JButton viewButton;
		private JButton editButton;
		private JButton detailButton;
		private JButton deleteButton;
		private JPanel jpAsgmContent;
		
		private CmsAssignment asgm;
		private JButton submissionButton;
		
		public AsgmEntryRow(final CmsAssignment asgm){
			this.asgm = asgm;
			
			JLabel jlTitle;
			JLabel postedDate; //avai date
			JLabel pdTitle;
			JLabel expDate; //exp date
			JLabel edTitle;
			
			
			//Set up file name label
			String asgmTitle = asgm.getAsgmTitle();
			jlTitle = new JLabel(asgmTitle);
			jlTitle.setFont(jlTitle.getFont().deriveFont(15.0f).deriveFont(Font.BOLD));
			jlTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 5, 5));
			
			//Set up the "Available date" title label
			pdTitle = new JLabel("Posted Date: ");
			pdTitle.setFont(pdTitle.getFont().deriveFont(10.0f).deriveFont(Font.BOLD));
			pdTitle.setBorder(BorderFactory.createEmptyBorder(5, 8, 0, 5));
			
			//set up avai date Label
			postedDate = new JLabel(CmsPresenter.sdf.format(new Date(asgm.getCreatedDate().getTime())));
			postedDate.setFont(jlTitle.getFont().deriveFont(10.0f));
			postedDate.setForeground(new Color(40,40,40));
			postedDate.setBorder(BorderFactory.createEmptyBorder(5, 5, 0, 5));
			
			//set up view button
			viewButton = new JButton ("View");
			viewButton.setFont(viewButton.getFont().deriveFont(12.0f));
			viewButton.setForeground(Color.BLUE);
			viewButton.setOpaque(false);
			viewButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			viewButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			deleteButton = new JButton ("Delete");
			deleteButton.setFont(viewButton.getFont().deriveFont(12.0f));
			deleteButton.setForeground(Color.BLUE);
			deleteButton.setOpaque(false);
			deleteButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			deleteButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up edit button
			editButton = new JButton ("Edit");
			editButton.setFont(viewButton.getFont().deriveFont(12.0f));
			editButton.setForeground(Color.BLUE);
			editButton.setOpaque(false);
			editButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			editButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			detailButton = new JButton ("Show Details");
			detailButton.setFont(viewButton.getFont().deriveFont(12.0f));
			detailButton.setForeground(Color.BLUE);
			detailButton.setOpaque(false);
			detailButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			detailButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//set up delete button
			submissionButton = new JButton ("Student Submissions");
			submissionButton.setFont(viewButton.getFont().deriveFont(12.0f));
			submissionButton.setForeground(Color.BLUE);
			submissionButton.setOpaque(false);
			submissionButton.setBorder(BorderFactory.createEmptyBorder(5, 5, 8, 8));
			submissionButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
			
			//Set up the panel
			setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
			setBackground(Color.WHITE);
			setBorder(BorderFactory.createLineBorder(new Color(170,170,170), 1));
			setOpaque(true);
			setLayout(new GridBagLayout());
			
			//add the Title
			GridBagConstraints cf = new GridBagConstraints();
			cf.fill = GridBagConstraints.HORIZONTAL;
			cf.anchor = GridBagConstraints.FIRST_LINE_START;
			cf.gridwidth = 4;
			add(jlTitle,cf);
			
			//add "Posted date" title:
			GridBagConstraints cp = new GridBagConstraints();
			cp.insets = new Insets(0,0,10,0);
			cp.gridx = 0;
			cp.gridy = 1;
			cp.anchor = GridBagConstraints.LAST_LINE_START;
			add(pdTitle,cp);
			
			//add the posted date
			GridBagConstraints cd = new GridBagConstraints();
			cd.insets = new Insets(0,0,10,0);
			cd.gridx = 1;
			cd.gridy = 1;
			cd.anchor = GridBagConstraints.LAST_LINE_START;
			add(postedDate,cd);
			
			
			//add the view button
			GridBagConstraints cb = new GridBagConstraints();
			cb.gridx = 1;
			cb.gridy = 2;
			cb.weightx = 1.0;
			cb.anchor = GridBagConstraints.LAST_LINE_END;
			add(viewButton,cb);
			
			//add the edit button
			GridBagConstraints cdel = new GridBagConstraints();
			cdel.gridx = 2;
			cdel.gridy = 2;
			cdel.anchor = GridBagConstraints.LAST_LINE_END;
			add(editButton,cdel);
			
			//student submission
			cdel = new GridBagConstraints();
			cdel.gridx = 3;
			cdel.gridy = 2;
			cdel.anchor = GridBagConstraints.LAST_LINE_END;
			add(submissionButton,cdel);
			
			
			//add the delete button
			GridBagConstraints c = new GridBagConstraints();
			c.gridx = 4;
			c.gridy = 2;
			c.anchor = GridBagConstraints.LAST_LINE_END;
			add(deleteButton,c);
			
			//add the delete button
			c = new GridBagConstraints();
			c.gridx = 5;
			c.gridy = 2;
			c.anchor = GridBagConstraints.LAST_LINE_END;
			add(detailButton,c);
			
			jpAsgmContent = new JPanel();
			c = new GridBagConstraints();
			c.gridx = 0;
			c.gridy = 3;
			c.gridwidth = 6;
			c.weighty = 1.0;
			c.insets = new Insets(0,10,10,10);
			c.fill = GridBagConstraints.VERTICAL;
			c.anchor = GridBagConstraints.FIRST_LINE_START;
			add(jpAsgmContent,c);
			
			//Add View AL:
			
			viewButton.addActionListener(new AsgmViewActionListener(asgm));
			deleteButton.addActionListener(new ALAssignmentDelete(asgm));
			submissionButton.addActionListener(new ALAssignmentViewSubmission(asgm));
			editButton.addActionListener(new ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					CmsPresenter.mainView.displayAssignmentEditPage(asgm);
				}
				
			});
			detailButton.addActionListener(new ShowDetailActionListener());
		}
		

		public class AsgmViewActionListener implements ActionListener{

			private CmsAssignment asgm;

			public AsgmViewActionListener(CmsAssignment asgm){
				this.asgm = asgm;
			}
			
			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.print("View Instructor Assignment: " + asgm.getAsgmTitle());
				CmsPresenter.mainView.displayAssignmentViewInstructorPage(asgm);
			}
			
		}
		
		public class ShowDetailActionListener implements ActionListener{

			@Override
			public void actionPerformed(ActionEvent e) {
				if(detailButton.getText().equals("Show Details")){
					detailButton.setText("Hide Details");
					
					GridBagLayout gridBagLayout = new GridBagLayout();
					gridBagLayout.columnWidths = new int[]{131, 154, 163, 0, 0};
					gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
					gridBagLayout.columnWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
					gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, Double.MIN_VALUE};
					jpAsgmContent.setLayout(gridBagLayout);
					jpAsgmContent.setOpaque(false);
					
					JLabel lblAvailableDate = new JLabel("Available Date: ");
					lblAvailableDate.setFont(new Font("Dialog", Font.BOLD, 12));
					GridBagConstraints gbc_lblAvailableDate = new GridBagConstraints();
					gbc_lblAvailableDate.anchor = GridBagConstraints.LINE_START;
					gbc_lblAvailableDate.insets = new Insets(10, 20, 5, 5);
					gbc_lblAvailableDate.gridx = 0;
					gbc_lblAvailableDate.gridy = 0;
					jpAsgmContent.add(lblAvailableDate, gbc_lblAvailableDate);
					
					JLabel jlbAvaiDate = new JLabel(CmsPresenter.sdf.format(new Date(asgm.getAvaiDate().getTime())));
					GridBagConstraints gbc_jlbAvaiDate = new GridBagConstraints();
					gbc_jlbAvaiDate.anchor = GridBagConstraints.LINE_START;
					gbc_jlbAvaiDate.insets = new Insets(10, 0, 5, 5);
					gbc_jlbAvaiDate.gridx = 1;
					gbc_jlbAvaiDate.gridy = 0;
					jpAsgmContent.add(jlbAvaiDate, gbc_jlbAvaiDate);
					
					JLabel lblDueDate = new JLabel("Due Date: ");
					lblDueDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblDueDate = new GridBagConstraints();
					gbc_lblDueDate.anchor = GridBagConstraints.LINE_START;
					gbc_lblDueDate.insets = new Insets(0, 20, 5, 5);
					gbc_lblDueDate.gridx = 0;
					gbc_lblDueDate.gridy = 1;
					jpAsgmContent.add(lblDueDate, gbc_lblDueDate);
					
					JLabel jlbDueDate = new JLabel(CmsPresenter.sdf.format(new Date(asgm.getDueDate().getTime())));
					GridBagConstraints gbc_jlbDueDate = new GridBagConstraints();
					gbc_jlbDueDate.anchor = GridBagConstraints.LINE_START;
					gbc_jlbDueDate.insets = new Insets(0, 0, 5, 5);
					gbc_jlbDueDate.gridx = 1;
					gbc_jlbDueDate.gridy = 1;
					jpAsgmContent.add(jlbDueDate, gbc_jlbDueDate);
					
					JLabel lblPercentWeight = new JLabel("Percent Weight: ");
					lblPercentWeight.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblPercentWeight = new GridBagConstraints();
					gbc_lblPercentWeight.anchor = GridBagConstraints.LINE_START;
					gbc_lblPercentWeight.insets = new Insets(0, 20, 5, 5);
					gbc_lblPercentWeight.gridx = 2;
					gbc_lblPercentWeight.gridy = 0;
					jpAsgmContent.add(lblPercentWeight, gbc_lblPercentWeight);
					
					JLabel jlbWeight = new JLabel("" + asgm.getPercentWeight() + "%");
					GridBagConstraints gbc_jlbWeight = new GridBagConstraints();
					gbc_jlbWeight.anchor = GridBagConstraints.LINE_START;
					gbc_jlbWeight.insets = new Insets(0, 0, 5, 0);
					gbc_jlbWeight.gridx = 3;
					gbc_jlbWeight.gridy = 0;
					jpAsgmContent.add(jlbWeight, gbc_jlbWeight);
					
					JLabel lblExpirationDate = new JLabel("Expiration Date: ");
					lblExpirationDate.setFont(new Font("Lucida Grande", Font.BOLD, 13));
					GridBagConstraints gbc_lblExpirationDate = new GridBagConstraints();
					gbc_lblExpirationDate.anchor = GridBagConstraints.LINE_START;
					gbc_lblExpirationDate.insets = new Insets(0, 20, 10, 5);
					gbc_lblExpirationDate.gridx = 0;
					gbc_lblExpirationDate.gridy = 2;
					jpAsgmContent.add(lblExpirationDate, gbc_lblExpirationDate);
					
					JLabel jlbExpDate = new JLabel(CmsPresenter.sdf.format(new Date(asgm.getExpDate().getTime())));
					GridBagConstraints gbc_jlbExpDate = new GridBagConstraints();
					gbc_jlbExpDate.anchor = GridBagConstraints.LINE_START;
					gbc_jlbExpDate.insets = new Insets(0, 0, 10, 5);
					gbc_jlbExpDate.gridx = 1;
					gbc_jlbExpDate.gridy = 2;
					jpAsgmContent.add(jlbExpDate, gbc_jlbExpDate);
					
					
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+90));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT+90));
					
					jpAsgmContent.revalidate();
				}
				else{
					detailButton.setText("Show Details");
					jpAsgmContent.removeAll();
					setPreferredSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setMaximumSize(new Dimension(ROW_WIDTH, ROW_HEIGHT));
					setBackground(Color.WHITE);
					jpAsgmContent.revalidate();
				}
				
			}
			
		}

	}
}
